package com.ljl.module.system.util;

import java.io.InputStream;
import java.util.Scanner;
import java.util.function.Function;

/**
 * @author 20664
 */
public class Utils {

    public static String getStreamMsg(InputStream is, Function<String, String> func) {
        return getStreamMsg(is, func, false, null);
    }
    public static void getStreamMsg(InputStream is, Function<String, String> func, Callback<String> callback) {
        getStreamMsg(is, func, true, callback);
    }

    private static String getStreamMsg(InputStream is, Function<String, String> func, boolean greediness, Callback<String> callback) {
        Scanner scanner = new Scanner(is, "UTF-8");
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String apply = func.apply(line);
            if (apply != null) {
                if (greediness) {
                    callback.call(apply);
                } else {
                    return apply;
                }
            }
        }
        return null;
    }
}
